<%@page pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/include.jsp"  %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <title>Go to shop generation form</title>
    <c:url var="jquery_custom_css" value="/ui/jquery-ui-1.8.4.custom.css" />
    <link href="${jquery_custom_css}" rel="stylesheet" type="text/css" />
    <c:url var="jquery_treeview_css" value="/css/jquery.treeview.css" />
    <link href="${jquery_treeview_css}" rel="stylesheet" type="text/css" />
    <c:url var="housewifes_css" value="/css/housewifes.css" />
    <link href="${housewifes_css}" rel="stylesheet" type="text/css" />

    <c:url var="jquery_js" value="/script/jquery-1.4.2.js" />
    <script type="text/javascript" src="${jquery_js}"></script>
    <c:url var="jquery_cookie_js" value="/script/jquery.cookie.js" />
    <script type="text/javascript" src="${jquery_cookie_js}"></script>
    <c:url var="jquery_treeview_js" value="/script/jquery.treeview.js" />
    <script type="text/javascript" src="${jquery_treeview_js}"></script>
    <c:url var="jquery_custom_js" value="/script/jquery-ui-1.8.4.custom.min.js" />
    <script type="text/javascript" src="${jquery_custom_js}"></script>

    <c:url var="hw_js" value="/script/housewifes.js" />
    <script type="text/javascript" src="${hw_js}"></script>
</head>
<body>
<c:import url="../pagemenus/mainUserMenu.jsp"/>
<br/>
Go to shop generation form
<br/>

<div class="select_period">
    <table>
        <tr>
            <td>
                <input id="go_shop_period_from">
            </td>
            <td>
                <input id="go_shop_period_to">
            </td>
            <td>
                <button id="apply_selected_period_btn">apply</button>
            </td>
        </tr>
    </table>
</div>

<div class="select_usertables" style="display:none;">
</div>

<div>
    <button id="submit_report_generation" disabled="true">Generate Report</button>
</div>

<div class="generated_report_container" style="display: none;">

</div>

<div id="loading_dialog" style="display:none;">
    <img src="img/loading.gif" style="text-align:center;" width="100%" height="100%" />
</div>

<script type="text/javascript">

    function checkDataSelectedForReport() {
        var userTableSelected = false;
        $(".report_usertable_check").each(function() {
            if (this.checked) {
                userTableSelected = true;
            }
        });

        var recipesSelected = false;
        // TODO: handle selected recipes here

        return userTableSelected || recipesSelected;
    }


    $(document).ready(function() {

        $("#apply_selected_period_btn").click(function() {
            var dateFrom = $("#go_shop_period_from").val();
            var dateTo = $("#go_shop_period_to").val();
            if (!dateFrom || !dateTo) {
                alert("select time period from & to");
                return;
            }
            showLoadingDialog();

            try {
                $.ajax({
                    url: "<c:url value="/get-available-users-tables-json.html"/>",
                    type: 'GET',
                    dataType: 'json',
                    data: { "from": dateFrom, "to": dateTo },
                    success: function(reply) {
                        if (reply) {
                            if (reply.error) {
                                alert('error:' + reply.error);
                            } else {
                                //alert(reply['formHtml']);
                                $(".select_usertables").html(reply['formHtml']);
                                $(".select_usertables").show();

                                if (checkDataSelectedForReport()) {
                                    $("#submit_report_generation").attr('disabled', false);
                                } else {
                                    $("#submit_report_generation").attr('disabled', true);
                                }
                            }
                        } else {
                            alert("Server error");
                        }
                        hideLoadingDialog();
                    },
                    error: function(error) {
                        alert(error);
                        window.location.reload(true);
                    }
                });
            } catch(ex) {
                alert(ex.name)
            }
        });

        $("#submit_report_generation").click(function() {

            var dateFrom = $("#go_shop_period_from").val();
            var dateTo = $("#go_shop_period_to").val();

            var userTableInputs = '';
            var userTablesIds = new Array();
            var indx = 0;
            $(".report_usertable_check").each(function() {
                if (this.checked) {
//                    userTableInputs = userTableInputs + '<input name="userTablesIds" value="' + $(this).val() + '">';
                    userTablesIds[indx] = $(this).val();
                    indx++;
                }
            });

            showLoadingDialog();
            $.ajax({
                url: "<c:url value="/generate-go-shop-report-json.html"/>",
                type: 'POST',
                dataType: 'json',
                data: { "from": dateFrom, "to": dateTo,  "userTablesIds": userTablesIds},
                success: function(reply) {
                    if (reply) {
                        if (reply.error) {
                            alert('error:' + reply.error);
                        } else {
                            //alert(reply['formHtml']);
                            $(".generated_report_container").html(reply['formHtml']);
                            $(".generated_report_container").show();
                        }
                    } else {
                        alert("Server error");
                    }
                    hideLoadingDialog();
                },
                error: function(error) {
                    alert(error);
                    window.location.reload(true);
                }
            });

        })

    });

</script>

</body>
</html>